package com.sgb.kjwl.view.ui.adapter.business;

import android.content.Context;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.sgb.kjwl.BR;
import com.sgb.kjwl.R;
import com.sgb.kjwl.model.entity.ApplyEntity;
import com.sgb.kjwl.model.entity.business.TripEntity;
import com.swgk.core.recyclerview.adapter.BaseRecycleAdapter;
import com.swgk.core.recyclerview.adapter.ItemViewHolder;

import java.util.List;

@Deprecated
public class TripAdapter extends BaseRecycleAdapter<TripEntity> {

    private LinearLayout llStartTime;
    private LinearLayout llEndTime;
    private LinearLayout llStartCity;
    private LinearLayout llEndCity;
    private TextView tvAddTrip;
    private TextView tvCancelTrip;
    private ImageView ivCancelTrip;
    private TextView tvNum;
    private TextView tvPlane;
    private TextView tvTrain;
    private TextView tvCar;
    private TextView tvOthers;
    private TextView etTripType;
    private TextView tvStartTime;
    private TextView tvEndTime;
    private TextView tvTimeLong;
    private TextView tvStartCity;
    private TextView tvEndCity;

    public TripAdapter(Context context, List<TripEntity> datas) {
        super(context, R.layout.item_trip, datas);
    }

    @Override
    public void convert(ItemViewHolder holder, final TripEntity entity) {
        holder.setBinding(BR.viewModel, entity);
        llStartTime = holder.getItem().findViewById(R.id.ll_startTime);
        llEndTime = holder.getItem().findViewById(R.id.ll_endTime);
        llStartCity = holder.getItem().findViewById(R.id.ll_startCity);
        llEndCity = holder.getItem().findViewById(R.id.ll_endCity);
        tvAddTrip = holder.getItem().findViewById(R.id.tv_addTrip);
        tvCancelTrip = holder.getItem().findViewById(R.id.tv_cancelTrip);
        ivCancelTrip = holder.getItem().findViewById(R.id.iv_cancelTrip);
        tvPlane = holder.getItem().findViewById(R.id.tv_plane);
        tvTrain = holder.getItem().findViewById(R.id.tv_train);
        tvCar = holder.getItem().findViewById(R.id.tv_car);
        tvOthers = holder.getItem().findViewById(R.id.tv_others);
        etTripType = holder.getItem().findViewById(R.id.et_tripType);
        tvNum = holder.getItem().findViewById(R.id.tv_num);
        tvStartTime = holder.getItem().findViewById(R.id.tv_startTime);
        tvEndTime = holder.getItem().findViewById(R.id.tv_endTime);
        tvTimeLong = holder.getItem().findViewById(R.id.tv_timeLong);
        tvStartCity = holder.getItem().findViewById(R.id.tv_startCity);
        tvEndCity = holder.getItem().findViewById(R.id.tv_endCity);

        tvStartTime.setText(entity.getStartTime());
        tvEndTime.setText(entity.getEndTime());
        tvTimeLong.setText(entity.getDuration());
        tvStartCity.setText(entity.getDepartCity());
        tvEndCity.setText(entity.getDestinCity());

        if ("1".equals(entity.getTransportNo())){
            tvOthers.setTextColor(context.getResources().getColor(R.color.color_grey_999999));
            tvOthers.setBackground(context.getResources().getDrawable(R.drawable.corner_grey_bg2));
            tvCar.setTextColor(context.getResources().getColor(R.color.color_grey_999999));
            tvCar.setBackground(context.getResources().getDrawable(R.drawable.corner_grey_bg2));
            tvTrain.setTextColor(context.getResources().getColor(R.color.color_grey_999999));
            tvTrain.setBackground(context.getResources().getDrawable(R.drawable.corner_grey_bg2));
            tvPlane.setTextColor(context.getResources().getColor(R.color.color_0286DF));
            tvPlane.setBackground(context.getResources().getDrawable(R.drawable.corner8_0286df_bg));
            etTripType.setVisibility(View.GONE);
        }else if ("2".equals(entity.getTransportNo())){
            tvOthers.setTextColor(context.getResources().getColor(R.color.color_grey_999999));
            tvOthers.setBackground(context.getResources().getDrawable(R.drawable.corner_grey_bg2));
            tvCar.setTextColor(context.getResources().getColor(R.color.color_grey_999999));
            tvCar.setBackground(context.getResources().getDrawable(R.drawable.corner_grey_bg2));
            tvTrain.setTextColor(context.getResources().getColor(R.color.color_0286DF));
            tvTrain.setBackground(context.getResources().getDrawable(R.drawable.corner8_0286df_bg));
            tvPlane.setTextColor(context.getResources().getColor(R.color.color_grey_999999));
            tvPlane.setBackground(context.getResources().getDrawable(R.drawable.corner_grey_bg2));
            etTripType.setVisibility(View.GONE);
        }else if ("3".equals(entity.getTransportNo())){
            tvOthers.setTextColor(context.getResources().getColor(R.color.color_grey_999999));
            tvOthers.setBackground(context.getResources().getDrawable(R.drawable.corner_grey_bg2));
            tvCar.setTextColor(context.getResources().getColor(R.color.color_0286DF));
            tvCar.setBackground(context.getResources().getDrawable(R.drawable.corner8_0286df_bg));
            tvTrain.setTextColor(context.getResources().getColor(R.color.color_grey_999999));
            tvTrain.setBackground(context.getResources().getDrawable(R.drawable.corner_grey_bg2));
            tvPlane.setTextColor(context.getResources().getColor(R.color.color_grey_999999));
            tvPlane.setBackground(context.getResources().getDrawable(R.drawable.corner_grey_bg2));
            etTripType.setVisibility(View.GONE);
        }else if ("".equals(entity.getTransportNo())){
            tvOthers.setTextColor(context.getResources().getColor(R.color.color_0286DF));
            tvOthers.setBackground(context.getResources().getDrawable(R.drawable.corner8_0286df_bg));
            tvCar.setTextColor(context.getResources().getColor(R.color.color_grey_999999));
            tvCar.setBackground(context.getResources().getDrawable(R.drawable.corner_grey_bg2));
            tvTrain.setTextColor(context.getResources().getColor(R.color.color_grey_999999));
            tvTrain.setBackground(context.getResources().getDrawable(R.drawable.corner_grey_bg2));
            tvPlane.setTextColor(context.getResources().getColor(R.color.color_grey_999999));
            tvPlane.setBackground(context.getResources().getDrawable(R.drawable.corner_grey_bg2));
            etTripType.setVisibility(View.VISIBLE);
        }

        etTripType.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable editable) {
                entity.setTransportName(etTripType.getText().toString());
            }
        });


        tvOthers.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                editClickListener.onOtherClick(entity, holder.getPosition()-1);
            }
        });

        tvPlane.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                editClickListener.onPlaneClick(entity, holder.getPosition()-1);
            }
        });

        tvTrain.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                editClickListener.onTrainClick(entity, holder.getPosition()-1);
            }
        });

        tvCar.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                editClickListener.onCarClick(entity, holder.getPosition()-1);
            }
        });

        llStartTime.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                editClickListener.onStartTimeClick(entity,holder.getPosition()-1);
            }
        });

        llEndTime.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                editClickListener.onEndTimeClick(entity, holder.getPosition()-1);
            }
        });

        llStartCity.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                editClickListener.onStartCityClick(entity, holder.getPosition()-1);
            }
        });

        llEndCity.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                editClickListener.onEndCityClick(entity, holder.getPosition()-1);
            }
        });

        tvAddTrip.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                getDatas().add(new TripEntity());
                notifyDataSetChanged();
            }
        });

        tvCancelTrip.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                getDatas().remove(holder.getAdapterPosition()-1);
                notifyDataSetChanged();
            }
        });

        ivCancelTrip.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                getDatas().remove(holder.getAdapterPosition()-1);
                notifyDataSetChanged();
            }
        });

        tvNum.setText("行程（"+holder.getPosition()+"）");

        if (holder.getPosition()>1){
            tvCancelTrip.setVisibility(View.VISIBLE);
            ivCancelTrip.setVisibility(View.VISIBLE);
        }else {
            tvCancelTrip.setVisibility(View.GONE);
            ivCancelTrip.setVisibility(View.GONE);
        }

        if (holder.getPosition() == getItemCount()){
            tvAddTrip.setVisibility(View.VISIBLE);
        }else {
            tvAddTrip.setVisibility(View.GONE);
        }

    }

    private TripAdapter.onEditClickListener editClickListener;

    public void setonEditClick(TripAdapter.onEditClickListener editClickListener) {
        this.editClickListener = editClickListener;
    }

    public interface onEditClickListener {
        void onStartTimeClick(TripEntity entity, int position);
        void onEndTimeClick(TripEntity entity, int position);
        void onStartCityClick(TripEntity entity, int position);
        void onEndCityClick(TripEntity entity, int position);
        void onPlaneClick(TripEntity entity, int position);
        void onTrainClick(TripEntity entity, int position);
        void onCarClick(TripEntity entity, int position);
        void onOtherClick(TripEntity entity, int position);

    }
}
